<template>
	<view class="container">
		<u-navbar title="我的钱包" leftIconColor="#fff" :autoBack="true" placeholder bgColor="transparent"></u-navbar>
		<view class="pad-32">
			<view v-for="item in payList" class="item mar-bottom-24">
				<template>
					<image class="radius24" v-if="item.icon" :src="item.icon" mode=""></image>
					<view class="color-white font-28 button"
						@click.stop="hanldeWallet(item.id,item.withdrawal_fee_rate)" v-if="item.isShow">进入钱包
					</view>
					<view @click="hanldePayList(item.id)" class="font-28 button" v-else>去开通</view>
				</template>
			</view>
		</view>

		<my-popup content="您还没实名认证？" confirmText="去认证" :show="isPopup" @cancel="isPopup = false"
			@confirm="hanldeConfirm"></my-popup>
	</view>
</template>

<script>
	import {
		payPlat,
		openWallet,
		userWalletInfo,
		userWalletOPen,
		realNameAuthInfo,
		addOpenWalletOrder,
		cashierPay
	} from '@/config/api.js'
	export default {
		data() {
			return {
				// 平台所有钱包
				payList: [],
				// 实名认证
				isPopup: false,
			}
		},
		onLoad() {
			this.getPayPlat()
		},
		methods: {
			// 开通钱包
			hanldePayList(platId) {
				realNameAuthInfo().then(res => {
					if (res.data.info.status == 2) {
						uni.showLoading({
							title: '开通中...'
						})

						userWalletOPen({
							plat_id: platId
						}).then(res => {
							let {
								open_data,
								need_pay_open_fee
							} = res.data;

							uni.hideLoading();
							if (open_data != "") {
								// 跳转获取手续费
								if (need_pay_open_fee) {
									this.getJump(platId, open_data);
								} else {
									if (platId == 13) {
										userWalletInfo({
											plat_id: 13
										}).then(res => {
											uni.navigateTo({
												url: '/pages/kft/wallet'
											})
										}).catch(err => {
											uni.showModal({
												title: '提醒',
												content: '去开通快付通',
												success: (res) => {
													if (res.confirm) {
														uni.navigateTo({
															url: '/pages/kftPay/kftPay'
														})
													}
												},

											})
										})
									} else if (platId == 11 || platId == 10) {
										// #ifdef APP-PLUS
										uni.navigateTo({
											url: '/pages/web-view/web-view?url=' + open_data
												.redirect_url
										})
										// #endif
										// #ifdef H5
										window.location.href = open_data.redirect_url
										// #endif
									}
								}
							} else {
								uni.$u.toast('正在对接，敬请期待~');
							}
						}).catch(err=>{
							this.getPayPlat()
						})
					} else {
						this.isPopup = true;
					}
				})
			},
			// 确认
			hanldeConfirm() {
				this.isPopup = false;
				uni.navigateTo({
					url: '/pages/my/certification'
				})
			},
			// 封装开通手续费
			getJump(platId, open_data) {
				uni.showModal({
					title: '提醒',
					content: open_data,
					success: (res) => {
						if (res.confirm) {
							
							// 快付通收取手续费
							if(platId == 13){
								uni.navigateTo({
									url: '/pages/kft/procedures'
								})
								return
							}
							
							addOpenWalletOrder({
								plat_id: platId
							}).then(order => {
								cashierPay({
									order_id: order.data.order_id,
									pay_plat: platId,
									return_url: 'https://h5.xuenuo8.cn/pages/my/wallet?jump=page'
								}).then(pay => {
									let url = pay.data.pay_data.pay_url;
									// #ifdef APP-PLUS

									if (platId == 11) {
										plus.runtime.openURL(
											`https://h5.xuenuo8.cn/llPay.html?url=${encodeURIComponent(url)}`
										)
									} else {
										uni.navigateTo({
											url: '/pages/web-view/web-view?url=' + url
										})
									}
									// #endif
									// #ifdef H5
									window.location.href = url
									// #endif
								})
							})
						}
					},

				})
			},
			// 我的钱包
			hanldeWallet(id, feeRate) {
				if (id == 11) {
					uni.setStorageSync('llfeeRate', feeRate);
					uni.navigateTo({
						url: '/pages/my/llWallet'
					})
				} else if (id == 13) {
					uni.navigateTo({
						url: '/pages/kft/wallet'
					})
				} else {
					userWalletInfo({
						plat_id: id
					}).then(res => {
						if (id != 11) {
							// #ifdef APP-PLUS
							uni.navigateTo({
								url: '/pages/web-view/web-view?url=' + res.data.info.redirect_url
							})
							// #endif
							// #ifdef H5
							window.location.href = res.data.info.redirect_url
							// #endif
						}
					})
				}
			},
			// 获取平台支付方式
			getPayPlat() {
				payPlat({
					scene: 'market'
				}).then(pay => {
					let {
						lists
					} = pay.data;

					// 获取自己开通的钱包
					openWallet().then(res => {
						
						let {
							yes
						} = res.data;
						lists.map((item, index) => {
							item.isShow = false;
							for (let key in yes) {
								if (item.id == key) {
									item.isShow = true;
								}
							}
						})
						this.payList = lists;
					})
				})
			},
		}
	}
</script>

<style scoped lang="scss">
	.container {
		height: 100vh;

		.item {
			position: relative;
		}

		.item>image {
			display: block;
			width: 100%;
			height: 300rpx;
		}

		.button {
			background: rgba(255, 255, 255, .5);
			position: absolute;
			bottom: 0rpx;
			right: 0rpx;
			padding: 18rpx 60rpx;
			border-radius: 24rpx 0;
		}
	}
</style>